<template>
  <div class="bg">
    <div class="login">
      <el-card header="内容管理后台登录" class="title">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item prop="username">
            <el-input
              type="text"
              placeholder="请输入用户名"
              required="required"
              v-model="ruleForm.username"
              prefix-icon="el-icon-user-solid"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              v-model="ruleForm.password"
              @keyup.enter.native="toSubmitForm('ruleForm')"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="submitForm('ruleForm')"
              style="width: 100%"
              >登录</el-button
            >
          </el-form-item>
           <el-form-item>
              <el-button type="info" @click="resetLoginForm('ruleForm')" style="width: 100%"
                >重置</el-button
              >
            </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<style lang="scss">
.login {
  .el-form-item__content {
    // margin-left: 0px ;
    margin: 0px 50px !important;
  }
  position: absolute;
  left: 0px;
  right: 0px;
  margin: 30px auto;
}
</style>

<style scoped>
.login {
  width: 450px;
  /* border: 1px solid; */
}
.el-card {
  background-color: rgba(245, 255, 255, 0.8);
}
.bg {
  width: 100%;
  height: 200vh;
  background: url(~@/assets/background.png);
  background-size: 100%;
}

.title {
  text-align: center;
  font-size: 30px;
}
</style>

<script>
import { login } from '../../src/api/user'
import { setToken } from '../utils/auth'

export default {
  data () {
    return {
      ruleForm: {
        password: '',
        username: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '用户名不能为空',
            trigger: ['blur', 'change']
          },
          {
            pattern: /^[0-9a-zA-Z_]{1,}$/,
            message: '用户名格式不正确，必须是字母数字或下划线',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          {
            required: true,
            message: '密码不能为空',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },

  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          login(this.ruleForm)
            .then((res) => {
              if (res.data.code === 200) {
                setToken(res.data.data.token)
                this.$router.back()
                setTimeout(() => {
                  this.$router.go(0)
                }, 300)
              } else {
                this.$validator({ type: 'error', validator: res.msg })
              }
            })
            .catch((err) => {
              console.log(err)
            })
        } else {
          console.log('error submit !!')
          return false
        }
      })
    },

    // 点击重置按钮，重置登录表单：resetFields
    resetLoginForm () {
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>
